package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.TodayIndexAdapter;
import flc.ast.bean.StarsBean;
import flc.ast.databinding.ActivityStarDetailBinding;
import stark.common.apis.ApiManager;
import stark.common.apis.base.ConstInfoBean;
import stark.common.apis.base.ConstTodayBean;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;

public class StarDetailActivity extends BaseAc<ActivityStarDetailBinding> {

    public static StarsBean starsBean;
    private TodayIndexAdapter todayIndexAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_star_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivStarDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivStarOverview.setOnClickListener(this);

        mDataBinding.rvTodayList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        todayIndexAdapter = new TodayIndexAdapter();
        mDataBinding.rvTodayList.setAdapter(todayIndexAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivStarOverview://星座总览
                startActivity(StarOverviewActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        getConstToday();
        getStarsAnalysis();
    }

    //获取今日势运
    private void getConstToday() {
        ApiManager.constApi().getConstToday(this, starsBean.getName(), new IReqRetCallback<ConstTodayBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable ConstTodayBean result) {
                if (success) {
                    setTodayData(result);
                } else {
                    ToastUtils.showShort(getString(R.string.is_limit));
                }
            }
        });
        setTextAndImg();
    }

    //设置今日势运
    private void setTodayData(ConstTodayBean result) {
        mDataBinding.tvLove.setText(getString(R.string.love, result.getLove()));//爱情
        mDataBinding.tvWealth.setText(getString(R.string.wealth, result.getMoney()));//财运
        mDataBinding.tvCause.setText(getString(R.string.cause, result.getWork()));//事业
        mDataBinding.tvHealth.setText(getString(R.string.health, result.getHealth()));//健康
        int all = Integer.parseInt(result.getAll()) / 20;
        List<Boolean> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            if (i <= all) {
                list.add(new Boolean(true));
            } else {
                list.add(new Boolean(false));
            }
        }
        todayIndexAdapter.setList(list);
        mDataBinding.sbLove.setProgress(Integer.parseInt(result.getLove()));
        mDataBinding.sbWealth.setProgress(Integer.parseInt(result.getMoney()));
        mDataBinding.sbCause.setProgress(Integer.parseInt(result.getWork()));
        mDataBinding.sbHealth.setProgress(Integer.parseInt(result.getHealth()));
    }

    //获取星座解析
    private void getStarsAnalysis() {
        if (!TextUtils.isEmpty(starsBean.getName())) {
            ApiManager.constApi().getConstInfo(this, starsBean.getName(), new IReqRetCallback<ConstInfoBean>() {
                @Override
                public void onResult(boolean success, String msg, @Nullable ConstInfoBean result) {
                    if (success) {
                        setAnalysisData(result);
                    } else {
                        ToastUtils.showShort(getString(R.string.is_limit));
                    }
                }
            });
        }
    }


    //设置星座解析
    private void setAnalysisData(ConstInfoBean result) {
        mDataBinding.tvNum.setText(result.getXyhm());
        mDataBinding.tvColor.setText(result.getXyys().substring(0, 1));
        mDataBinding.tvSxsx.setText(result.getSssx());
        mDataBinding.tvYysx.setText(result.getYysx());
        mDataBinding.tvZgxx.setText(result.getZgxx());
        mDataBinding.tvZggw.setText(result.getZggw());
        mDataBinding.tvJxsw.setText(result.getJssw());
        mDataBinding.tvKyjs.setText(result.getKyjs());
        mDataBinding.tvAnalysis.setText(result.getJttz());
    }

    private void setTextAndImg() {
        mDataBinding.ivStarImg.setImageResource(starsBean.getImg());
        mDataBinding.tvStarName.setText(starsBean.getName());
        mDataBinding.tvStarBirthDate.setText(starsBean.getDate());
    }

}